package com.wbk.lotterysystemsystemservice.service.serviceImpl;

import cn.hutool.core.util.RandomUtil;
import com.wbk.lotterysystemsystemservice.service.EmailService;
import com.wbk.system.qo.LotteryAuditResultQO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import com.wbk.system.qo.StoreAuditResultQO;

import javax.mail.internet.MimeMessage;
import java.util.concurrent.TimeUnit;

@Service
public class EmailServiceImpl implements EmailService {

    public static final String FROM = "353964055@qq.com";

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    JavaMailSenderImpl javaMailSender;

    @Override
    public void sendCode(String email) throws Exception {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
        String code = RandomUtil.randomNumbers(6);
        message.setSubject("邮箱验证码-FORT");
        String html = "<html>" +
                "<h2>FORT-邮箱绑定邮件</h2>\n" +
                "<p>亲爱的FORT用户:</p>\n" +
                "<p>请复制下面的验证码，并返回原页面提交以完成邮箱绑定。</p>\n" +
                "<p>" + code + "</p>\n" +
                "<p>验证码有效期为5分钟</p>\n" +
                "<p>如非本人操作请忽略本邮件。</p>" +
                "</html>";
        message.setText(html, true);
        message.setTo(email);
        message.setFrom(FROM);
        javaMailSender.send(mimeMessage);
        //将验证码放入redis
        stringRedisTemplate.opsForValue().set("code:" + email, code, 5, TimeUnit.MINUTES);
    }

    @Override
    public void storeAuditResult(StoreAuditResultQO storeAuditResultQO) throws Exception {
        String result = null;
        if (storeAuditResultQO.getAuditStatus() == 1) {
            result = "审核通过";
        }
        if (storeAuditResultQO.getAuditStatus() == 2) {
            result = "审核不通过，请重新修改相关信息:";
        }
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
        message.setSubject("申请商家认证结果通知-FORT");
        String html = "<html>" +
                "<h2>FORT-申请商家认证邮件</h2>\n" +
                "<p>亲爱的FORT用户:</p>\n" +
                "<p>您申请的商家认证审核结果如下：</p>\n" +
                "<h3>" + result + "</h3>\n" +
                "<h3>"+storeAuditResultQO.getAuditOpinion()+"</h3>\n" +
                "<p>若有疑问可与管理员联系。</p>" +
                "<p>如非本人操作请忽略本邮件。</p>" +
                "</html>";
        message.setText(html, true);
        message.setTo(storeAuditResultQO.getEmail());
        message.setFrom(FROM);
        javaMailSender.send(mimeMessage);
    }

    @Override
    public void lotteryAuditResult(LotteryAuditResultQO lotteryAuditResultQO) throws Exception {
        String result = null;
        if (lotteryAuditResultQO.getAuditStatus() == 1) {
            result = "审核通过";
        }
        if (lotteryAuditResultQO.getAuditStatus() == -2) {
            result = "审核不通过，请重新修改相关信息:";
        }
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
        message.setSubject("抽奖活动审核结果通知-FORT");
        String html = "<html>" +
                "<h2>FORT-抽奖活动审核邮件</h2>\n" +
                "<p>亲爱的FORT用户:</p>\n" +
                "<p>您创建的抽奖活动审核结果如下：</p>\n" +
                "<h3>" + result + "</h3>\n" +
                "<h3>"+lotteryAuditResultQO.getAuditOpinion()+"</h3>\n" +
                "<p>若有疑问可与管理员联系。</p>" +
                "<p>如非本人操作请忽略本邮件。</p>" +
                "</html>";
        message.setText(html, true);
        message.setTo(lotteryAuditResultQO.getEmail());
        message.setFrom(FROM);
        javaMailSender.send(mimeMessage);
    }
}
